using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class LibUniWinApi
{
    // 定义用于字符串和整数回调的委托类型
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate void StringCallback([MarshalAs(UnmanagedType.LPWStr)] string returnString); // 字符串回调委托，使用LPWStr进行封送

    // 整数回调委托，使用I4进行封送
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    public delegate void IntCallback([MarshalAs(UnmanagedType.I4)] int value);


    // 状态检查相关的外部方法
    // 导入LibUniWinC库中的函数，指定调用约定为Windows API默认调用约定
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    // 指定返回值的类型为bool，并使用MarshalAs属性确保正确封送
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsActive(); // 检查窗口是否处于活动状态

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsTransparent(); // 检查窗口是否透明

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsBorderless(); // 检查窗口是否无边框

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsTopmost(); // 检查窗口是否置顶

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsBottommost(); // 检查窗口是否置底

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsMaximized(); // 检查窗口是否最大化

    // 窗口附加相关的外部方法
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool AttachMyWindow(); // 附加到当前窗口

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool AttachMyOwnerWindow(); // 附加到所有者窗口

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool AttachMyActiveWindow(); // 附加到活动窗口

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool DetachWindow(); // 分离窗口

    // 窗口更新相关的外部方法
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void Update(); // 更新窗口

    // 窗口样式设置相关的外部方法
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetTransparent([MarshalAs(UnmanagedType.U1)] bool bEnabled); // 设置窗口透明

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetBorderless([MarshalAs(UnmanagedType.U1)] bool bEnabled); // 设置窗口无边框

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetAlphaValue(float alpha); // 设置窗口透明度值

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetClickThrough([MarshalAs(UnmanagedType.U1)] bool bEnabled); // 设置窗口点击穿透

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetTopmost([MarshalAs(UnmanagedType.U1)] bool bEnabled); // 设置窗口置顶

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetBottommost([MarshalAs(UnmanagedType.U1)] bool bEnabled); // 设置窗口置底

    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetMaximized([MarshalAs(UnmanagedType.U1)] bool bZoomed); // 设置窗口最大化

    // 窗口位置和大小相关的外部方法
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetPosition(float x, float y);

    // 以下代码定义了一系列用于与LibUniWinC库交互的Windows API函数调用
    // 这些函数主要用于获取和设置窗口的位置、大小、鼠标位置等信息
    // 以及注册各种回调函数和处理窗口相关的功能
    // 获取窗口位置
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetPosition(out float x, out float y);

    // 设置窗口大小
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetSize(float x, float y);

    // 获取窗口大小
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetSize(out float x, out float y);

    // 获取客户区域大小
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetClientSize(out float width, out float height);

    // 获取客户区域矩形
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetClientRectangle(out float x, out float y, out float width, out float height);

    // 注册文件拖放回调函数
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool RegisterDropFilesCallback([MarshalAs(UnmanagedType.FunctionPtr)] StringCallback callback);

    // 注销文件拖放回调函数
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool UnregisterDropFilesCallback();

    // 注册显示器变化回调函数
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool RegisterMonitorChangedCallback([MarshalAs(UnmanagedType.FunctionPtr)] IntCallback callback);

    // 注销显示器变化回调函数
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool UnregisterMonitorChangedCallback();

    // 注册窗口样式变化回调函数
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool RegisterWindowStyleChangedCallback([MarshalAs(UnmanagedType.FunctionPtr)] IntCallback callback);

    // 注销窗口样式变化回调函数
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool UnregisterWindowStyleChangedCallback();

    // 设置是否允许拖放
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetAllowDrop([MarshalAs(UnmanagedType.U1)] bool enabled);

    // 获取当前显示器索引
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern int GetCurrentMonitor();

    // 获取显示器数量
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern int GetMonitorCount();

    // 获取指定显示器的矩形区域
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetMonitorRectangle(int index, out float x, out float y, out float width, out float height);

    // 设置鼠标光标位置
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetCursorPosition(float x, float y);

    // 获取鼠标光标位置
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetCursorPosition(out float x, out float y);

    // 获取鼠标按钮状态
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern int GetMouseButtons();

    // 获取修饰键状态
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern int GetModifierKeys();

    #region Windows 专用功能
    // Windows平台特有的功能区域
    // 设置窗口透明类型
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetTransparentType(int type);

    // 设置窗口透明颜色
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern void SetKeyColor(uint colorref);

    // 获取调试信息
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    public static extern int GetDebugInfo();

    //附加窗口句柄
    [DllImport("LibUniWinC", CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool AttachWindowHandle(IntPtr hWnd);
    #endregion

}
